﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EnvironmentManagerSystem.BLL;
using EnvironmentManagerSystem.MODE;


namespace EnvironmentManagerSystem.RoleManager
{
    public partial class RoleRight : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {
                BindRole();
                if (DDLRole.SelectedIndex != -1)
                {
                    BindRoleRight(int.Parse(DDLRole.SelectedValue.ToString()));
                    BindRight();
                }
            }

        }

        /// <summary>
        /// 绑定角色
        /// </summary>
        protected void BindRole()
        {
            DDLRole.DataSource = Role.GetList();
            DDLRole.DataTextField = "RoleName";
            DDLRole.DataValueField = "ID";
            DDLRole.DataBind();
        }

        protected void BindRoleRight(int roleid)
        {
            LBRoleRight.DataSource = Function.GetList(roleid);
            LBRoleRight.DataTextField = "name";
            LBRoleRight.DataValueField = "ID";
            LBRoleRight.DataBind();
        }

        /// <summary>
        /// 绑定权限
        /// </summary>
        protected void BindRight()
        {
            LBRight.DataSource = Function.GetList();
            LBRight.DataTextField = "name";
            LBRight.DataValueField = "ID";
            LBRight.DataBind();
            foreach (ListItem list in LBRoleRight.Items)
            {
                LBRight.Items.Remove(LBRight.Items.FindByValue(list.Value));
            }
        }
        protected void DDLRole_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindRoleRight(int.Parse(DDLRole.SelectedValue.ToString()));
            BindRight();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            if (this.LBRoleRight.SelectedIndex != -1)
            {
                ListItem m_Item = new ListItem(this.LBRoleRight.SelectedItem.Text, this.LBRoleRight.SelectedItem.Value);

                Function.DeleteRoleFunction(int.Parse(DDLRole.SelectedValue), int.Parse(LBRoleRight.SelectedValue));
                if (!isTrue(LBRight, LBRoleRight.SelectedItem.Value))
                {
                    this.LBRight.Items.Add(m_Item);
                }
                this.LBRoleRight.Items.Remove(m_Item);
                if (LBRoleRight.Items.Count > 0)
                    LBRoleRight.SelectedIndex = 0;
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (this.LBRight.SelectedIndex != -1)
            {
                ListItem m_Item = new ListItem(this.LBRight.SelectedItem.Text, this.LBRight.SelectedItem.Value);
                if (!isTrue(LBRoleRight, LBRight.SelectedItem.Value))
                {
                    Function.InsertRoleFunction(int.Parse(DDLRole.SelectedValue), int.Parse(LBRight.SelectedValue));
                    this.LBRoleRight.Items.Add(m_Item);
                }
                this.LBRight.Items.Remove(m_Item);
                if (LBRight.Items.Count > 0)
                    LBRight.SelectedIndex = 0;
            }
        }

        /// <summary>
        /// 是否有重复
        /// </summary>
        /// <param name="lb">ListBoxID</param>
        /// <param name="Value">对比值</param>
        /// <returns>bool值</returns>
        private bool isTrue(ListBox lb, string Value)
        {
            bool m_temp = false;
            for (int i = 0; i < lb.Items.Count; i++)
            {
                if (lb.Items[i].Value == Value)
                {
                    return m_temp = true;
                }
            }
            return m_temp;
        }
    }
}
